﻿// <copyright file="BoolTag.cs" company="Signal Processing Lab, Wuhan University">
// Copyright (c) 2009-2010 All Right Reserved
// </copyright>
// <author>Xinping Deng</author>
// <email>dentoby@gmail.com</email>
// <date>2010-4</date>
// <summary>This is part of Image.NET project.</summary>

namespace SPLWHU.Core
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The <see cref="Tag"/> with a value of boolean type.
    /// </summary>
    public class BoolTag : Tag<bool>
    {
        /// <summary>
        /// The observer.
        /// </summary>
        private List<Tag> observers;

        /// <summary>
        /// Initializes a new instance of the <see cref="BoolTag"/> class.
        /// </summary>
        /// <param name="name">The name of this tag.</param>
        public BoolTag(string name)
            : this(name, false)
        {
            this.ValueChanged += new ValueChanged(BoolTag_ValueChanged);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BoolTag"/> class.
        /// </summary>
        /// <param name="name">The name of this tag.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public BoolTag(string name, bool value)
            : base(name, value)
        {
            this.ValueChanged += new ValueChanged(BoolTag_ValueChanged);
        }

        /// <summary>
        /// Converts the value of this instance to text.
        /// </summary>
        /// <returns>Converted text representing the value.</returns>
        public override string ValueToText()
        {
            return this.Value.ToString();
        }

        /// <summary>
        /// Parses the value of this instance from text.
        /// </summary>
        /// <param name="text">The text to be parsed to generate value.</param>
        public override void ValueFromText(string text)
        {
            try
            {
                this.Value = bool.Parse(text);
            }
            catch
            {
                throw new ParseException("Can not parse a bool from " + text);
            }
        }

        /// <summary>
        /// Registers a <see cref="Tag"/> as an observer of this instance.
        /// </summary>
        /// <param name="observer">The <see cref="Tag"/> to add .</param>
        public void RegisterObserver(Tag observer)
        {
            if (this.observers == null)
            {
                this.observers = new List<Tag>();
            }

            this.observers.Add(observer);
        }

        /// <summary>
        /// Remove a <see cref="Tag"/> as an observer of this instnce.
        /// </summary>
        /// <param name="observer">The <see cref="Tag"/> to remove.</param>
        public void UnregisterObserver(Tag observer)
        {
            if (this.observers != null)
            {
                this.observers.Remove(observer);
            }
        }

        /// <summary>
        /// Clears the registered observers.
        /// </summary>
        public void ClearRegister()
        {
            if (this.observers != null)
            {
                this.observers.Clear();
            }
        }

        /// <summary>
        /// Enumerates the observers registered.
        /// </summary>
        /// <returns>The enumerator of the registered observers.</returns>
        public IEnumerable<Tag> EnumerateObserver()
        {
            if (this.observers == null)
            {
                return null;
            }

            return this.observers;
        }

        /// <summary>
        /// The value changed event hander.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event arguments.</param>
        private void BoolTag_ValueChanged(object sender, ValueChangedArgs e)
        {
            if (this.observers != null)
            {
                // Fires the event.
                foreach (Tag observer in this.observers)
                {
                    observer.ReadOnly = this.Value;
                }
            }
        }
    }
}
